<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>我的购物车</title>
	<link rel="icon" href="http://static.xiaohong.work/img/favicon.ico">

	<!-- 引入样式 -->
	<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
	<link rel="stylesheet" type="text/css" href="http://static.xiaohong.work/css/all.css" />
	<link rel="stylesheet" type="text/css" href="http://static.xiaohong.work/css/pages-cart.css" />
</head>

<body>
<div id="app">
	<!--head-->
	<!-- 头部栏位 -->
	<!--页面顶部-->
	<div>
		<top/>
	</div>


	<div class="cart py-container">
		<!--All goods-->
		<div class="allgoods">
			<h4>全部商品
				<span></span>
			</h4>
			<div class="cart-main">
				<div class="yui3-g cart-th">
					<div class="yui3-u-1-4">
						<input class="chooseAll" type="checkbox" name="" id="" value=""  v-model="allCheck"/> 全部</div>
					<div class="yui3-u-1-4">商品</div>
					<div class="yui3-u-1-4">描述</div>
					<div class="yui3-u-1-8">单价（元）</div>
					<div class="yui3-u-1-8">数量</div>
					<div class="yui3-u-1-8">小计（元）</div>
					<div class="yui3-u-1-8">操作</div>
				</div>
				<div class="cart-item-list">
					<div class="cart-body">
						<!-- vue loadlist -->
						<div class="cart-list" v-for="(item,index) in shopcarlist" :key="index">
							<ul class="goods-list yui3-g">
								<li class="yui3-u-1-24">
									<input type="checkbox" v-model="choiceList" :value="item" name="chk_list" id="" value="" />
								</li>
								<li class="yui3-u-6-24">
									<div class="good-item">
										<div class="item-img">
											<img :src="item.image" style="width: 100px;height: 90px" />
										</div>
									</div>
								</li>
								<li class="yui3-u-5-24">
									<div class="item-msg" v-text="item.name"></div>
								</li>
								<li class="yui3-u-1-8">
									<span class="price" v-text="item.price"></span>
								</li>
								<li class="yui3-u-1-8">
									<a @click="decr(item)" class="increment mins" v-show="item.num > 1">-</a>
									<input autocomplete="off" type="text" :value="item.num" @change="change($event,item)" minnum="1" class="itxt" />
									<a @click="incr(item)" class="increment plus">+</a>
								</li>
								<li class="yui3-u-1-8">
									<span class="sum" v-text="item.money"></span>
								</li>
								<li class="yui3-u-1-8">
									<a href="#none" @click="deleteCart(item,index)">删除</a>
									<br />
									<a href="#none">移到收藏</a>
								</li>
							</ul>
						</div>

					</div>
				</div>
			</div>
			<div class="cart-tool">
				<div class="select-all">
					<input class="chooseAll" type="checkbox" name="" id="" value="" v-model="allCheck"/>
					<span>全选</span>
				</div>
				<div class="option">
					<a href="#none">删除选中的商品</a>
					<a href="#none">移到我的关注</a>
					<a href="#none">清除下柜商品</a>
				</div>
				<div class="money-box">
					<div class="chosed">已选择
						<span>0</span>件商品</div>
					<div class="sumprice">
						<span>
							<em>总价（不含运费） ：</em>
							<i class="summoney">¥<em v-text="sum"></em></i>
						</span><br>
						<span>
							<em>已节省：</em>
							<i>-¥<em v-text="sum-paySum"></em></i>
						</span>
					</div>
					<div class="sumbtn" >
						<a class="sum-btn" @click="order" target="_blank">结算</a>
					</div>
				</div>
			</div>
			<div class="clearfix"></div>
			<div class="liked">
				<ul class="sui-nav nav-tabs">
					<li class="active">
						<a href="#index" data-toggle="tab">猜你喜欢</a>
					</li>
					<li>
						<a href="#profile" data-toggle="tab">特惠换购</a>
					</li>
				</ul>
				<div class="clearfix"></div>
				<div class="tab-content">
					<div id="index" class="tab-pane active">
						<div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
							<div class="carousel-inner">
								<div class="active item">
									<ul>
										<li>
											<img src="http://static.xiaohong.work/img/like1.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like2.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like3.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like4.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
									</ul>
								</div>
								<div class="item">
									<ul>
										<li>
											<img src="http://static.xiaohong.work/img/like1.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like2.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like3.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like4.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
									</ul>
								</div>
							</div>
							<a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
							<a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
						</div>
					</div>
					<div id="profile" class="tab-pane">
						<p>特惠选购</p>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- 底部栏位 -->
	<!--页面底部-->
	<div>
		<end/>
	</div>
	<!--页面底部END-->
</div>
	<script type="text/javascript" src="http://static.xiaohong.work/js/plugins/jquery/jquery-3.5.1.min.js"></script>
	<!-- 引入组件库 -->
	<script src="https://cdn.jsdelivr.net/npm/vue@2.6.8/dist/vue.js"></script>
	<script src="http://static.xiaohong.work/js/axios.min.js"></script>
	<script src="http://static.xiaohong.work/js/common.js"></script>


	<script type="text/javascript">
		$(".chooseAll").click(function () {
			$("input[name='chk_list']").prop("checked", $(this).prop("checked"));
		})
	</script>
	<script>
		new Vue({
			el: '#app',
			data() {
				return {
					shopcarlist: [],
					choiceList: [],
					sum: 0 ,   //应付金额
					paySum: 0,  //实付金额
					user: JSON.parse(window.localStorage.getItem('user')),
					allCheck: false,
					unCheck_flag: true,
				}
			},
			created: function () {
				this.getCart();
			},
			methods: {
				order(){
					//判断勾选商品是否为空
					if (xiaohong.store.get("Cars").length === 0){
						alert("请勾选需要购买的商品！")
					}else {
						window.location = 'http://www.xiaohong.work/order.html';
					}
				},
				change(e,cart){
					if (e.target.value >= 1){
						let cha = e.target.value - cart.num;
						cart.num = e.target.value;
						cart.money = cart.num* cart.price;
						cart.payMoney = cart.money;
						//获取商品在列表中的下标
						if (this.choiceList.length === 0){
							//没有商品
							xiaohong.store.set("Cars",[]);
						}else {
							//获取下标
							let index = -1;
							for (let i = 0; i <this.choiceList.length; i++) {
								if(this.choiceList[i].id === cart.id){
									index = i;
								}
							}

							if (index === -1){
								//说明此时没有选中商品
								xiaohong.store.set("Cars",this.choiceList);
							}else{
								this.choiceList.splice(index,1,cart);
								xiaohong.store.set("Cars",this.choiceList);
							}
						}
						xiaohong.http.put('/cart/update?id='+cart.id+"&num="+cha).then(resp => {
							if(resp.data.code === 20000){
								this.getCart();
								// this.shopcarlist = resp.data.data.date;
								// this.choiceList = xiaohong.store.get("Cars");
							}
						}).catch(() => alert('更新购物车失败'));
					}else {
						alert('参数错误！')
					}
				},
				getCart() {
					xiaohong.http.get('/cart/list').then(async resp => {
						if(resp.data.code === 20000){
							this.shopcarlist = resp.data.data;
							if (xiaohong.store.get("Cars") === null){
								this.choiceList = this.shopcarlist;
							}else{
								this.choiceList = xiaohong.store.get("Cars");
							}
						}
					}).catch(() => alert('获取购物车失败'));
				},
				incr(cart){
					cart.num++;
					cart.money += cart.price;
					cart.payMoney = cart.money;
					//获取商品在列表中的下标
					if (this.choiceList.length === 0){
						//没有商品
						xiaohong.store.set("Cars",[]);
					}else {
						//获取下标
						let index = -1;
						for (let i = 0; i <this.choiceList.length; i++) {
							if(this.choiceList[i].id === cart.id){
								index = i;
							}
						}

						if (index === -1){
							//说明此时没有选中商品
							xiaohong.store.set("Cars",this.choiceList);
						}else{
							this.choiceList.splice(index,1,cart);
							xiaohong.store.set("Cars",this.choiceList);
						}
					}
					xiaohong.http.put('/cart/update?id='+cart.id+"&num=1").then(resp => {
						if(resp.data.code === 20000){
							this.getCart();
							// this.shopcarlist = resp.data.data.date;
							// this.choiceList = xiaohong.store.get("Cars");
						}
					}).catch(() => alert('更新购物车失败'));
				},
				decr(cart){
					cart.num --;
					cart.money -= cart.price;
					cart.payMoney = cart.money;
					// this.$set(this.choiceList[cart.index], `payMoeny`, cart.money)
					//获取商品在列表中的下标
					if (this.choiceList.length === 0){
						//没有商品
						xiaohong.store.set("Cars",[]);
					}else {
						//获取下标
						let index = -1;
						for (let i = 0; i <this.choiceList.length; i++) {
							if(this.choiceList[i].id === cart.id){
								index = i;
							}
						}

						if (index === -1){
							//说明此时没有选中商品
							xiaohong.store.set("Cars",this.choiceList);
						}else{
							this.choiceList.splice(index,1,cart);
							xiaohong.store.set("Cars",this.choiceList);
						}
					}
					xiaohong.http.put('/cart/update?id=' + cart.id + "&num=-1").then(resp => {
						if (resp.data.code === 20000) {
							//查询购物车
							this.getCart();
							// this.shopcarlist = resp.data.data.date;
							// this.choiceList = xiaohong.store.get("Cars");
						}
					}).catch(() => alert('更新购物车失败'));
				},
				deleteCart(cart,sindex){
					if (this.choiceList.length === 0){
						//没有商品
						xiaohong.store.set("Cars",[]);
					}else {
						//获取下标
						let index = -1;
						for (let i = 0; i <this.choiceList.length; i++) {
							if(this.choiceList[i].id === cart.id){
								index = i;
							}
						}

						if (index === -1){
							//说明此时没有选中该商品
						}else{
							//选中了该商品
							this.choiceList.splice(index,1);
							xiaohong.store.set("Cars",this.choiceList);
						}
					}
					xiaohong.http.put('/cart/update?id=' + cart.id + "&num=-"+cart.num).then(resp => {
						if (resp.data.code === 20000) {
							//查询购物车
							// this.shopcarlist = resp.data.data.data;
							this.getCart();
						}
					}).catch(() => alert('删除购物车失败'));
				},
				toOrder(){
					ly.verify().then(res=>{
						ly.store.set("LY_SELECTED",this.selected);
						window.location ="http://www.xiaohong.com/getOrderInfo.html";
					}).catch(()=>{
						window.location = "http://www.xiaohong.com/login.html?returnUrl=http://www.xiaohong.com/cart.html";
					})
				}
			},
			watch:{
				allCheck(newVal){
					if (newVal){
						this.choiceList = this.shopcarlist;
						xiaohong.store.set("Cars",this.choiceList);
						this.sum = 0;
						this.paySum = 0;
						for (let i = 0; i <this.choiceList.length; i++) {
							this.sum += this.choiceList[i].money;
							this.paySum += this.choiceList[i].payMoney;
						}
					}else{
						if(this.unCheck_flag){
							this.choiceList = [];
							xiaohong.store.set("Cars",this.choiceList);
							this.sum = 0;
							this.paySum = 0;
						}else {
							this.sum = 0;
							this.paySum = 0;
							xiaohong.store.set("Cars",this.choiceList);
							for (let i = 0; i <this.choiceList.length; i++) {
								this.sum += this.choiceList[i].money;
								this.paySum += this.choiceList[i].payMoney;
							}
						}
					}

				},
				choiceList(newVal){
					// newVal === this.shopcarlist 好像有bug,即使一样了也没反应。。。
					if (newVal.length !== this.shopcarlist.length){
						this.unCheck_flag = false;
						this.allCheck = false;
					}else {
						this.unCheck_flag = true;
						this.allCheck = true;
					}
					this.sum = 0;
					this.paySum = 0;
					xiaohong.store.set("Cars",this.choiceList);
					for (let i = 0; i <this.choiceList.length; i++) {
						this.sum += this.choiceList[i].money;
						this.paySum += this.choiceList[i].payMoney;
					}

				}
			},
			components: {
				top: () => import("http://static.xiaohong.work/js/pages/top.js"),
				end: () => import("http://static.xiaohong.work/js/pages/end.js")
			}
		})
	</script>

</body>

</html>
